package com.charlie.sessions;

import com.charlie.beans.Product;
import com.charlie.beans.Users;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Stateful;

/**
 *
 * @author Vitaliy Mostipaka <imprtat@gmail.com>
 */
@Stateful
public class Cart implements CartLocal {

    public class CartItem {

        private int itemId;
        private int amount;

        public CartItem(int itemId, int amount) {
            this.itemId = itemId;
            this.amount = amount;
        }

        public int getId() {
            return itemId;
        }

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        @Override
        public int hashCode() {
            return itemId;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final CartItem other = (CartItem) obj;
            if (this.itemId != other.itemId) {
                return false;
            }
            return true;
        }
    }
    
    @EJB
    private ProductFacadeLocal products;
    
    private Set<CartItem> items;
    private String email;
    private String location;
    private int warehouseId;
    private boolean notify;
    private static int maxCost = 0;

    public static void setMaxCost(int cost) {
        maxCost = cost;
    }
    
    public static int getMaxCost() {
        return maxCost;
    }
    
    @PostConstruct
    @Override
    public void init() {
        items = new LinkedHashSet<CartItem>();
        email = "";
        location = "";
        warehouseId = 0;
        notify = true;
    }

    @PreDestroy
    @Override
    public void destroy() {
        items = null;
    }

    @Override
    public CartItem findItemById(int itemId) {
        for (CartItem item : items) {
            if (item.getId() == itemId) {
                return item;
            }
        }
        return null;
    }

    @Override
    public int getCost() {
        int sum = 0;
        for (CartItem item : items) {
            Product product = products.find(item.getId());
            sum += item.getAmount() * product.getCost();
        }
        return sum;
    }

    @Override
    public void addItem(int itemId, int amount) {
        CartItem item = findItemById(itemId);
        if (amount > 0) {
            if (item == null) {
                Product product = products.find(itemId);
                amount = Math.min(amount, (maxCost - getCost()) / product.getCost());
                if (product.getStatus() == 1 && amount > 0) {
                    item = new CartItem(product.getProductId(), amount);
                    items.add(item);
                }
            } else {
                setItemAmount(itemId, amount + item.getAmount());
            }
        }
    }

    @Override
    public void setItemAmount(int itemId, int amount) {
        if (amount == 0) {
            removeItem(itemId);
        } else if (amount > 0) {
            CartItem item = findItemById(itemId);
            if (item != null) {
                Product product = products.find(itemId);
                if (product != null) {
                    int oldItemCost = product.getCost() * item.getAmount();
                    amount = Math.min(amount, (maxCost - getCost() + oldItemCost) / product.getCost());
                    item.setAmount(amount);
                }
            }
        }
    }

    @Override
    public void removeItem(int itemId) {
        for (CartItem item : items) {
            if (item.getId() == itemId) {
                items.remove(item);
                break;
            }
        }
    }

    @Override
    public void clear() {
        items.clear();
    }

    @Override
    public Set<CartItem> getItems() {
        return items;
    }

    @Override
    public int getItemsAmount() {
        int sum = 0;
        for (CartItem item : items) {
            sum += item.getAmount();
        }
        return sum;
    }

    @Override
    public void setUser(Users user) {
        if (user == null) {
            return;
        }
        if (email == null || email.length() == 0) {
            setEmail(user.getEmail());
        }
        setLocation(user.getLocation());
    }

    @Override
    public String getEmail() {
        return email;
    }

    @Override
    public String getLocation() {
        return location;
    }

    @Override
    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public void setWarehouse(int warehouseId) {
        this.warehouseId = warehouseId;
    }

    @Override
    public int getWarehouse() {
        return warehouseId;
    }

    @Override
    public boolean getNotify() {
        return notify;
    }

    @Override
    public void setNotify(boolean notify) {
        this.notify = notify;
    }
}
